class IndexeddbService {
  open(dbName) {
    var request = window.indexedDB.open(dbName, 1); // 打开1版本数据库
    var db;
    return new Promise((resolve, reject) => {
      request.onerror = function (event) {
        console.log('数据库打开报错');
        reject();
      };

      request.onsuccess = function (event) {
        db = request.result;
        console.log('数据库打开成功');
        resolve(db);
      };

      request.onupgradeneeded = function (event) {
        db = event.target.result;
        resolve(db);
      };
    });
  }

  createTable(db, tableName) {
    var objectStore;
    if (!db.objectStoreNames.contains(tableName)) {
      objectStore = db.createObjectStore(tableName, {keyPath: 'id'});
    }
  }

  createIndex(objectStore, indexName, indexColumn, options = {unique: true}) {
    objectStore.createIndex(indexName, indexColumn, options);
  }

  add(db, tableName, row) {
    var request = db.transaction([tableName], 'readwrite')
      .objectStore(tableName)
      .add(row);

    return new Promise((resolve, reject) => {
      request.onsuccess = function (event) {
        console.log('数据写入成功');
        resolve();
      };

      request.onerror = function (event) {
        console.log('数据写入失败');
        reject();
      };
    });

  }


  read(tableName) {
    var transaction = db.transaction([tableName]);
    var objectStore = transaction.objectStore(tableName);
    var request = objectStore.get(1);

    return new Promise((resolve, reject) => {
      request.onerror = function (event) {
        console.log('事务失败');
        reject();
      };

      request.onsuccess = function (event) {
        if (request.result) {
          resolve(request.result);
        } else {
          console.log('未获得数据记录');
          resolve();
        }
      };
    });

  }


  readAll(tableName) {
    var objectStore = db.transaction(tableName).objectStore(tableName);
    let arr = [];
    return new Promise((resolve, reject) => {
      objectStore.openCursor().onsuccess = function (event) {
        var cursor = event.target.result;
        if (cursor) {
          arr.push(cursor);
          cursor.continue();
        } else {
          resolve(arr);
          console.log('没有更多数据了！');
        }
      };
    });

  }

  update(tableName, row) {
    var request = db.transaction([tableName], 'readwrite')
      .objectStore(tableName)
      .put(row);

    return new Promise((resolve, reject) => {
      request.onsuccess = function (event) {
        console.log('数据更新成功');
        resolve(row);
      };

      request.onerror = function (event) {
        console.log('数据更新失败');
        reject();
      };
    });

  }

  remove() {
    var request = db.transaction(['person'], 'readwrite')
      .objectStore('person')
      .delete(1);

    return new Promise((resolve, reject) => {
      request.onsuccess = function (event) {
        console.log('数据删除成功');
        resolve();
      };
    });

  }

  readByIdex(tableName, indexName, value) {
    var transaction = db.transaction([tableName], 'readonly');
    var store = transaction.objectStore(tableName);
    var index = store.index(indexName);
    var request = index.get(value);

    return new Promise((resolve, reject) => {
      request.onsuccess = function (e) {
        var result = e.target.result;
        if (result) {
          resolve(result);
        } else {
          resolve(reject);
        }
      };
    });

  }


}

window.IndexeddbService = new IndexeddbService();

export default window.IndexeddbService;